#include "mysql.h"

int rm(MYSQL *mysql, int peerfd, int user_id, char *path){
    if(path[0] != '/'){
        char temp[1024] = {0};
        strcpy(temp, path);
        char pwd[100] = {0};
        strcpy(pwd, Pwd(mysql, peerfd, user_id, pwd));
        sprintf(path, "%s/%s", pwd, temp);
    }
    if(path[0] == '/'){
        //删除当前目录下的文件
        printf("jinlaile\n");
        char pwd[100] = {0};
        //strcpy(pwd, Pwd(mysql, 0, user_id, pwd));
        strcpy(pwd, path);
        int id = findParentId(mysql, pwd);
        //printf("path:%s, id:%d\n", path, id);
        char where[SQL_MAX] = {0};
        sprintf(where, "id = %d", id);
        if(atoi(SearchTableData(mysql, "vrfile", "type", where)) == 1){
            char sql[SQL_MAX] = {0};
            snprintf(sql, SQL_MAX, "SELECT id FROM vrfile WHERE parent_id = %d", id);
            //printf("查询sql语句：%s\n", sql);
            int ret = mysql_query(mysql, sql);
            if (ret) {
                printf("查询表数据失败！失败原因：%s\n",
                       mysql_error(mysql));
                return -1;
            }
            //printf("查询表数据成功！\n");
            MYSQL_RES *result = mysql_store_result(mysql);
            if(result == NULL){
                printf("error query: %s\n", mysql_error(mysql));
                return -1;
            }
            int rows = mysql_num_rows(result);
            int cols = mysql_num_fields(result);
            //printf("rows:%d, cols:%d\n", rows, cols);

            MYSQL_ROW row;
            while((row = mysql_fetch_row(result)) != NULL){
                for(int i = 0; i < cols; ++i){
                    char *child[1024] = {0};
                    child[i] = row[i];
                    printf("child:%s\n", child[i]);
                    char where1[SQL_MAX] = {0};
                    sprintf(where1, "id = %d", atoi(child[i]));
                    if(atoi(SearchTableData(mysql, "vrfile", "type", where1)) == 1){
                        char newpath[1024] = {0};
                        //printf("newpath:%s\n", newpath);
                        sprintf(newpath, "%s/%s", path, SearchTableData(mysql, "vrfile", "filename", where1));
                        //if(strlen(newpath) == 0){
                        //    delTabledata(mysql, "vrfile", where1);
                        //    printf("删除成功!\n");
                        //}
                        printf("newpath:%s\n", newpath);
                        sleep(1);
                        rm(mysql, peerfd, user_id, newpath);
                    }
                    char delwhere[1024] = {0};
                    sprintf(delwhere, "id = %d", atoi(child[i]));
                    delTabledata(mysql, "vrfile", where1);
                    char *msg = "删除成功！\n";
                    sendMsg(peerfd, msg);
                }
            }
        }
        if(atoi(SearchTableData(mysql, "vrfile", "type", where)) == 0){
            delTabledata(mysql, "vrfile", where);
            //printf("删除成功!\n");
            char *msg = "删除成功！\n";
            sendMsg(peerfd, msg);
        }
            delTabledata(mysql, "vrfile", where);
            //printf("删除成功!\n");
            char *msg = "删除成功！\n";
            sendMsg(peerfd, msg);
    }
}
int main(){
    MYSQL *mysql;
    connectDB(&mysql);
    char path[1024] = {0};
    strcpy(path, "hi");
    int user_id = 14;
    printf("before rm\n");
    rm(mysql,0,  user_id, path);
    printf("after rm\n");
}
